package com.immortal.module.security.handler;

import com.immortal.core.module.res.ApiResult;
import com.immortal.core.utils.http.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class AuthenticationLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
    private static final ApiResult LOGIN_ERROR_RESULT = ApiResult.Custom().success();

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException {
        if (ServletUtils.isAjaxRequest(request)) {
            ServletUtils.renderResult(response, LOGIN_ERROR_RESULT);
        } else {
            super.onLogoutSuccess(request, response, auth);
        }
    }
}